package com.espirit.eap.util.tagAdaptor;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.espirit.eap.manager.I18nService;
import com.espirit.eap.pagelayout.Element;
import com.espirit.eap.pagelayout.ElementService;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.Layout;
import com.espirit.eap.pagelayout.PageCache;
import com.espirit.eap.pagelayout.TabbedSection;
import com.espirit.eap.pagelayout.TagInfo;
import com.googlecode.cswish.struts.RestAction;
import com.googlecode.cswish.struts.spring.GenericJPAService;
import com.googlecode.cswish.util.Functions;

@Service
public class TagTabbedSectionAdaptor implements TagElementAdaptor<TabbedSection> {

	private static final Log logger = LogFactory.getLog(GenericJPAService.class);
	
	@Resource
	private I18nService i18nService;
	
	@Resource
	private Functions functions;
	
	@Resource
	private TagAdaptorUtil tagAdaptorUtil;
	
	@Resource
	private ElementService elementService;
	
	@Override
	public TagInfo getTagProperties(Layout root, TabbedSection element, Map<String, Object> context) {
		TabbedSection tabbedSection = element;
		TagInfo tagInfo = new TagInfo(true);
		tagInfo.setTagName("tab");
		tagInfo.setContainer(true);
		Map<String, Object> properties = tagInfo.getTagProperties();
		String name = tagAdaptorUtil.getName(element);
		properties.put("name", name);
		
		// extra properties
		Map<String, String> exProperties = tabbedSection.getExProperties();
		if (exProperties != null) {
			properties.putAll(exProperties);
		}
		
		return tagInfo;
	}
	
	@Override
	public String getTagName(TabbedSection element) {
		return "tab";
	}
	
	@Override
	public TabbedSection updateToElement(Map<String, String> properties, TabbedSection element, Map<String, Object> context, PageCache pageCache) {
		TabbedSection tabbedSection = element;
		
		// TODO: update labels
		
		// add property: name, labels, links
		Map<String, Object> defaultTagProperties = (Map<String, Object>) context.get(DEFAULT_TAG_PROP);
		tagAdaptorUtil.updateElement(properties, defaultTagProperties, tabbedSection);
		
		return tabbedSection;
	}

	@Override
	public TabbedSection addElement(Element parent, String tagName, Map<String, Object> properties, PageCache pageCache) {
		Integer index = pageCache.increaseMaxId();
		
		TabbedSection tabbedSection = new TabbedSection();
		tabbedSection.setId(-index);
		
		// name
		String name = tagAdaptorUtil.createName(pageCache.getLayout(), tagName);
		tabbedSection.setName(name);
		
		FlatSection tabPage = elementService.insertPage(tabbedSection, 0);
		Element se = tabbedSection.getChildren().get(0);
		se.setId(-(index + 1));
		tabPage.setId(-(index + 2));
		
		// tab page name
		String tabPageName = tagAdaptorUtil.createName(pageCache.getLayout(), "tabpage");
		tabPage.setName(tabPageName);
		
		boolean isRead = RestAction.isRead(pageCache.getInvoker().methodName);
		if (isRead) {
			tabPage.setNamePrefix("execute");
		} else {
			tabPage.setNamePrefix("model");
		}
		
		// set the tab title
		tabPage.setI18n("新标签");
		
		Layout root = pageCache.getLayout();
		tagAdaptorUtil.addToParent(tabbedSection, root, parent, properties);
		
		return tabbedSection;
	}

	@Override
	public void deleteElement(Element parent, TabbedSection existed, PageCache pageCache) {
	}
};